package com.baizhi.service.impl;

import com.baizhi.entity.School;
import com.baizhi.dao.SchoolMapper;
import com.baizhi.service.FeignCommentService;
import com.baizhi.service.ISchoolService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhangsan
 * @since 2022-06-27
 */
@Service
public class SchoolServiceImpl extends ServiceImpl<SchoolMapper, School> implements ISchoolService {
    @Autowired
    //获取RedisTemplate工具
    private RedisTemplate<String,Object> redisTemplate;

    @Autowired
    private FeignCommentService fcs;
    @Override
    public School selectDetailSchool(int id) {
        School school = baseMapper.selectById(id);
        //调用redis获取数据
        ValueOperations<String, Object> vo = redisTemplate.opsForValue();
        Integer likes = (Integer)vo.get("school:" + id + ":likes");
        Integer views = (Integer)vo.get("school:"+id+":views");
        if(likes==null){
            likes=0;
        }
        if(views==null){
            views=0;
        }
        school.setLikes(likes);
        school.setViews(views);
        //给评论数赋值
        Integer countComment = fcs.selectNumForSchool(id);
        school.setCommentCount(countComment);
        return school;
    }
}
